package cn.fintecher.pangolin.service.report.task;

import cn.fintecher.pangolin.entity.elastic.BaseCase;
import cn.fintecher.pangolin.entity.elastic.HistoryCases;
import cn.fintecher.pangolin.entity.elastic.PaymentRecord;
import cn.fintecher.pangolin.entity.elastic.PrincipalPayment;
import cn.fintecher.pangolin.service.report.elastic.BaseCaseElastic;
import cn.fintecher.pangolin.service.report.elastic.HistoryCasesElastic;
import cn.fintecher.pangolin.service.report.elastic.PaymentRecordElastic;
import org.apache.commons.collections4.IterableUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * @Auther: xiaqun
 * @Description:
 * @Date: 15:18 2019/12/11
 */

@Service("principalPaymentTask")
public class PrincipalPaymentTask {
    @Autowired
    private BaseCaseElastic baseCaseElastic;

    @Autowired
    private HistoryCasesElastic historyCasesElastic;

    @Autowired
    private PaymentRecordElastic paymentRecordElastic;

    /**
     * 生成委托方回款报表
     */
    @Async
    public CompletableFuture<PrincipalPayment> doTask(PrincipalPayment principalPayment) {
        BoolQueryBuilder builder = new BoolQueryBuilder();
        builder.must(QueryBuilders.matchPhraseQuery("batchNumber.keyword", principalPayment.getBatchNumber()));
        Iterable<BaseCase> baseCaseIterable = baseCaseElastic.search(builder);
        Iterable<HistoryCases> historyCasesIterable = historyCasesElastic.search(builder);
        List<BaseCase> baseCases = IterableUtils.toList(baseCaseIterable);
        List<HistoryCases> historyCases = IterableUtils.toList(historyCasesIterable);
        List<String> caseIds = new ArrayList<>();
        double totalAmt = 0d;
        if (!baseCases.isEmpty()) {
            for (BaseCase baseCase : baseCases) {
                caseIds.add(baseCase.getId());
                totalAmt += baseCase.getOverdueAmtTotal();
            }
        }
        if (!historyCases.isEmpty()) {
            for (HistoryCases historyCase : historyCases) {
                caseIds.add(historyCase.getId());
                totalAmt += historyCase.getOverdueAmtTotal();
            }
        }
        principalPayment.setTotalAmt(totalAmt); //委案金额
        BoolQueryBuilder builder1 = new BoolQueryBuilder();
        builder1.must(QueryBuilders.matchPhraseQuery("flag", 0));
        builder1.must(QueryBuilders.termsQuery("caseId.keyword", caseIds));
        Iterable<PaymentRecord> paymentRecordIterable = paymentRecordElastic.search(builder1);
        List<PaymentRecord> paymentRecords = IterableUtils.toList(paymentRecordIterable);
        if (paymentRecords.isEmpty()) {
            principalPayment.setPaymentNumber(0); //回款笔数
            principalPayment.setPaymentAmt(0d); //回款金额
            principalPayment.setPaymentRate(0f); //回款率
        } else {
            int num = 0;
            double amt = 0d;
            for (PaymentRecord paymentRecord : paymentRecords) {
                num++;
                amt += paymentRecord.getPaymentAmt();
            }
            principalPayment.setPaymentNumber(num); //回款笔数
            principalPayment.setPaymentAmt(amt); //回款金额
            principalPayment.setPaymentRate(new BigDecimal(amt / totalAmt).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
        }
        return CompletableFuture.completedFuture(principalPayment);
    }
}
